package ms.mall.service;

import ms.common.exception.BizException;
import ms.common.util.bean.BeanUtil;
import ms.mall.bean.UserAddrParamBean;
import ms.mall.config.ResponseCode;
import ms.mall.dao.UserAddrDAO;
import ms.mall.dao.UserDAO;
import ms.mall.entity.User;
import ms.mall.entity.UserAddr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Project Name: ms
 * File Name: AddrService
 * Package Name: ms.mall.service
 * Date: 2018/6/4  15:36
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class AddrService {

    @Autowired
    private UserAddrDAO userAddrDAO;

    @Autowired
    private UserDAO userDAO;

    /**
     * 新增收货地址
     * @param userAddrParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void create(UserAddrParamBean userAddrParamBean) {
        User user = userDAO.selectByPrimaryKey(userAddrParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }
        UserAddr userAddr = BeanUtil.copy(userAddrParamBean, UserAddr.class);
        String detailAddr = userAddrParamBean.getProvinceName() + userAddrParamBean.getCityName() + userAddrParamBean.getAreaName() + userAddrParamBean.getAddr();
        userAddr.setDetailAddr(detailAddr);
        userAddr.setCreateTime(System.currentTimeMillis());

        userAddrDAO.insert(userAddr);
    }

    /**
     * 修改收货地址
     * @param userAddrParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void update(UserAddrParamBean userAddrParamBean) {
        User user = userDAO.selectByPrimaryKey(userAddrParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }

        UserAddr currentUserAddr = userAddrDAO.selectByPrimaryKey(userAddrParamBean.getId());
        if(null == currentUserAddr) {
            throw new BizException(ResponseCode.ADDR_UN_EXIST, "地址不存在");
        }

        UserAddr userAddr = BeanUtil.copy(userAddrParamBean, UserAddr.class);
        String detailAddr = userAddrParamBean.getProvinceName() + userAddrParamBean.getCityName() + userAddrParamBean.getAreaName() + userAddrParamBean.getAddr();
        userAddr.setDetailAddr(detailAddr);
        userAddr.setCreateTime(null);

        userAddrDAO.updateByPrimaryKeySelective(userAddr);
    }

    /**
     * 删除收货地址
     * @param userAddrParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void del(UserAddrParamBean userAddrParamBean) {
        User user = userDAO.selectByPrimaryKey(userAddrParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }
        userAddrDAO.deleteByPrimaryKey(userAddrParamBean.getId());
    }

    /**
     * 查看收获地址
     * @param userAddrParamBean
     * @return
     */
    public UserAddr detail(UserAddrParamBean userAddrParamBean) {
        User user = userDAO.selectByPrimaryKey(userAddrParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }
        UserAddr userAddr = userAddrDAO.selectByPrimaryKey(userAddrParamBean.getId());
        if(null == userAddr) {
            throw new BizException(ResponseCode.ADDR_UN_EXIST, "地址不存在");
        }
        return userAddr;
    }

    /**
     * 收获地址列表
     * @param userAddrParamBean
     * @return
     */
    public List<UserAddr> list(UserAddrParamBean userAddrParamBean) {
        User user = userDAO.selectByPrimaryKey(userAddrParamBean.getOpenid());
        if(null == user) {
            throw new BizException(ResponseCode.USER_UN_EXIST, "用户不存在");
        }

        UserAddr userAddr = new UserAddr();
        userAddr.setOpenid(userAddrParamBean.getOpenid());
        return userAddrDAO.selectByExample(userAddr);
    }
}
